// pages/equityCardShop/equityCardShop.js
const app = getApp()
import {
  postRequest,
  postParamsRequest,
} from '../../api/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    myEqCord1: app.globalData.myEqCord1,
    myEqCord2: app.globalData.myEqCord2,
    myEqCord3: app.globalData.myEqCord3,
    myEqCord4: app.globalData.myEqCord4,
    myEqCord5: app.globalData.myEqCord5,
    myCouponPackage: app.globalData.myCouponPackage,
    dataType: 0,
    tabList: [{
        id: 0,
        name: "购买卡券"
      },
      {
        id: 1,
        name: "我的卡券"
      },
    ],
    myCarType: 2,
    cardCur: 0,
    bannerData: '',
    couponList: [], //券包
    equityList: [], //卡包
    pageNo1: 1,
    pageSize1: 16,
    isDown: false,
    loadType1: '',
    pageNo2: 1,
    pageSize2: 10,
    loadType2: '',
    equityOrderList: [],
    pageNo3: 1,
    pageSize3: 10,
    loadType3: '',
    couponPackageOrderList: [],
  },

  //tab切换时
  tabSelect(e) {
    this.setData({
      dataType: e.currentTarget.dataset.id,
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    if (this.data.dataType == 0) {
      this.initData();
    } else {
      this.setData({
        pageNo2: 1,
        pageSize2: 10,
        loadType2: "",
        pageNo3: 1,
        pageSize3: 10,
        loadType3: "",
      })
      this.initData2();
    }
  },

  //卡片切换时
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },

  //我的卡类型切换时
  myCarTypeChange(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      myCarType: type,
      pageNo2: 1,
      pageSize2: 10,
      loadType2: "",
      pageNo3: 1,
      pageSize3: 10,
      loadType3: "",
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.initData2();
  },

  //获取轮播图
  getBannerList() {
    let that = this;

    postParamsRequest("/api/banner/list", {
      beid: app.beid,
      bannerType: 'card',
    }, 2).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        let bannerData = '';
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].bannerType == 'card' && res.data.data[i].isShow == 1 && res.data.data[i].bannerUrl != '') {
            bannerData = res.data.data[i];
            bannerData.bannerUrl = bannerData.bannerUrl.split(',');
            that.setData({
              bannerData: bannerData,
            })
            return;
          }
        }
        that.setData({
          bannerData: bannerData,
        })
      }

    }).catch((err) => {

    });

  },

  //初始化购买卡券数据
  async initData() {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    this.getBannerList();
    if (this.data.dataType == 0) {
      this.setData({
        isDown: false,
      })
      await this.getWxappCouponPackageList();
      await this.getWxappEquityCardList();
      this.setData({
        isDown: true,
      })
    } else {
      this.initData2()
    }

    let loadType1 = "";
    if (this.data.couponList.length == 0 && this.data.equityList.length == 0) {
      loadType1 = "loadNull";
    } else {
      loadType1 = "loadOver";
    }
    this.setData({
      loadType1: loadType1,
    })

    wx.hideLoading();
  },

  //初始我的卡券数据
  initData2() {
    let that = this;
    if (that.data.myCarType == 1) {
      that.getCouponPackageOrderList(false);
    } else {
      that.getEquityCardOrderList(false);
    }
  },

  //前往券包详情
  goCouponPackageDetail2(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../couponPackageDetail2/couponPackageDetail2?id=' + item.couponPackageId,
    })
  },

  //前往周期卡详情
  goCardDetail2(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../equityCardDetail2/equityCardDetail2?id=' + item.id,
    })
  },

  //获取周期卡订单列表,isConcat 是否拼接
  getEquityCardOrderList(isConcat) {
    let that = this;
    postRequest("/api/activity/getEquityCardOrderList", {
      beid: app.beid,
      memberId: app.myInfo.memberId,
      pageNo: that.data.pageNo2,
      pageSize: that.data.pageSize2,
    }).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        //合并数据
        let list = isConcat ? that.data.equityOrderList.concat(res.data.data.data) : res.data.data.data;
        console.log(list);
        //判断是否还有数据
        let loadType = "";
        if (res.data.data.totalCount == 0) {
          loadType = "loadNull";
        } else {
          if (Math.ceil(res.data.data.totalCount / that.data.pageNo2) <= that.data.pageSize2) {
            loadType = "loadOver";
          } else {
            loadType = "loadMore";
          }
        }
        list.forEach((item, index) => {
          //判断使用默认图片
          if (item.cardThumb == '') {
            if (item.cardType == 1) {
              item.cardThumb = that.data.myEqCord1;
            } else if (item.cardType == 2) {
              item.cardThumb = that.data.myEqCord2;
            } else if (item.cardType == 3) {
              item.cardThumb = that.data.myEqCord3;
            } else if (item.cardType == 4) {
              item.cardThumb = that.data.myEqCord4;
            } else if (item.cardType == 5) {
              item.cardThumb = that.data.myEqCord5;
            }
          }
        });
        that.setData({
          equityOrderList: list,
          loadType2: loadType,
          pageNo2: that.data.pageNo2 + 1,
        })
      }
    }).catch((err) => {

    });
  },

  //获取券包活动订单列表,isConcat 是否拼接
  getCouponPackageOrderList(isConcat) {
    let that = this;
    postRequest("/api/activity/getCouponPackageOrderList", {
      beid: app.beid,
      memberId: app.myInfo.memberId,
      pageNo: that.data.pageNo3,
      pageSize: that.data.pageSize3,
    }).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        //合并数据
        let list = isConcat ? that.data.couponPackageOrderList.concat(res.data.data.data) : res.data.data.data;
        console.log(list);
        //判断是否还有数据
        let loadType = "";
        if (res.data.data.totalCount == 0) {
          loadType = "loadNull";
        } else {
          if (Math.ceil(res.data.data.totalCount / that.data.pageNo3) <= that.data.pageSize3) {
            loadType = "loadOver";
          } else {
            loadType = "loadMore";
          }
        }
        list.forEach((item, index) => {
          //判断使用默认图片
          if (item.thumb == '') {
            item.thumb = that.data.myCouponPackage;
          }
        });
        that.setData({
          couponPackageOrderList: list,
          loadType3: loadType,
          pageNo3: that.data.pageNo3 + 1,
        })
      }
    }).catch((err) => {

    });
  },

  //获取券包
  getWxappCouponPackageList() {
    let that = this;
    return new Promise(function (resolve, reject) {
      postRequest("/api/activity/getWxappCouponPackageList", {
        beid: app.beid,
        memberId: app.myInfo.memberId,
        pageNo: that.data.pageNo1,
        pageSize: that.data.pageSize1,
      }).then((res) => {
        console.log(res.data.data);
        if (res.data.data != null) {

          //判断是否会员专享
          res.data.data.data.forEach((item, index) => {
            item.isVipHave = false;
            if (item.vipLevel != '' && item.vipLevel.indexOf('0') == -1) {
              item.isVipHave = true;
            }
            //判断使用默认图片
            if (item.thumb == '') {
              item.thumb = that.data.myCouponPackage;
            }
          });

          that.setData({
            couponList: res.data.data.data
          })
        }
        resolve(res);
      }).catch((err) => {
        reject(err);
      });
    })
  },

  //获取权益卡
  getWxappEquityCardList() {
    let that = this;
    return new Promise(function (resolve, reject) {
      postRequest("/api/activity/getWxappEquityCardList", {
        beid: app.beid,
        memberId: app.myInfo.memberId,
        pageNo: that.data.pageNo1,
        pageSize: that.data.pageSize1,
      }).then((res) => {
        console.log(res.data.data);
        if (res.data.data != null) {

          //判断是否会员专享
          res.data.data.data.forEach((item, index) => {
            item.isVipHave = false;
            if (item.scopeVip != '' && item.scopeVip.indexOf('0') == -1) {
              item.isVipHave = true;
            }
            //判断使用默认图片
            if (item.cardThumb == '') {
              if (item.cardType == 1) {
                item.cardThumb = that.data.myEqCord1;
              } else if (item.cardType == 2) {
                item.cardThumb = that.data.myEqCord2;
              } else if (item.cardType == 3) {
                item.cardThumb = that.data.myEqCord3;
              } else if (item.cardType == 4) {
                item.cardThumb = that.data.myEqCord4;
              } else if (item.cardType == 5) {
                item.cardThumb = that.data.myEqCord5;
              }
            }
          });
          that.setData({
            equityList: res.data.data.data
          })
        }
        resolve(res);
      }).catch((err) => {
        reject(err);
      });
    })
  },

  //前往购买券包
  goCPackage(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../couponPackageDetail/couponPackageDetail?id=' + id,
    })
  },

  //前往购买权益卡
  goECard(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../equityCardDetail/equityCardDetail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.dataType) {
      this.setData({
        dataType: options.dataType,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      pageNo2: 1,
      pageSize2: 10,
      loadType2: "",
      pageNo3: 1,
      pageSize3: 10,
      loadType3: "",
    })
    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.initData();
      }).catch(err => {})
    } else {
      this.initData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.dataType == 1 && this.data.myCarType == 2) {
      if (this.data.loadType2 == "loadMore") {
        this.setData({
          loadType2: "",
        });
        this.getEquityCardOrderList(true);
      }
    } else if (this.data.dataType == 1 && this.data.myCarType == 1) {
      if (this.data.loadType3 == "loadMore") {
        this.setData({
          loadType3: "",
        });
        this.getCouponPackageOrderList(true);
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})